import QtQuick 1.0
import com.nokia.meego 1.0
import "HarmattanUIConstants.js" as Constants

Item {
    id: gestureList

    property alias model: gestureModelView.model
    property alias gestureCount: gestureModelView.count
    property Item selectedItem: null
    property int selectedGestureId: -1

    signal contentMovementStarted()
    signal contentMovementEnded()
    signal itemSelected()
    signal itemPressed()

    // Inner signals
    signal play
    signal stop

    /**
     * Starts playing the animations.
     */
    function playAnimation()
    {
        console.debug("GestureList::playAnimation()");
        gestureList.play();
    }

    /**
     * Stops the animations.
     */
    function stopAnimation()
    {
        console.debug("GestureList::stopAnimation()");
        gestureList.stop();
    }

    /**
     * Deselects the selected item.
     */
    function deselect()
    {
        console.debug("GestureList::deselect()");

        if (selectedItem) {
            selectedItem.isSelected = false;
            selectedItem = null;
        }
    }

    GridView {
        id: gestureModelView
        width: parent.width //Math.floor(parent.width / cellWidth) * cellWidth
        height: parent.height
        anchors.centerIn: parent
        cellWidth: Constants.GESTURE_ITEM_SIZE +
                   Constants.DEFAULT_MARGIN
        cellHeight: cellWidth

        // Keep the items in memory.
        //cacheBuffer: 1500

        delegate: GestureListItem {
            id: gestureListItem
            width: Constants.GESTURE_ITEM_SIZE
            height: width

            Connections {
                target: gestureList
                onPlay: play();
                onStop: stop();
            }

            onPressed: {
                if (selectedItem) {
                    selectedItem.isSelected = false;
                    selectedItem = null;
                }

                gestureList.itemPressed();
            }

            onSelected: {
                console.debug("GestureList.qml: gestureListItem:onSelected:", id);

                if (selectedItem) {
                    selectedItem.isSelected = false;
                }

                gestureList.selectedGestureId = id;
                gestureList.selectedItem = gestureListItem;
                gestureList.itemSelected();
            }

            onModified: {
                console.debug("GestureList.qml: gestureListItem:onModified:", id)
                helper.setModified(id);
            }
        }

        onCountChanged: {
            if (count == 0) {
                helper.clearModifiedGestures();
            }
        }

        onMovementStarted: contentMovementStarted();
        onMovementEnded: contentMovementEnded();
    }

    ScrollDecorator {
        flickableItem: gestureModelView
    }
}

// End of file.
